﻿using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using ASPNETVN.PORTAL.Components.Configuration.Items;
using ASPNETVN.PORTAL.Components.Security;

namespace ASPNETVN.PORTAL.Controls
{
    public class NavigationBar : WebControl, INamingContainer
    {
        protected override void Render(HtmlTextWriter writer)
        {
            Portal portal = (Portal)HttpContext.Current.Items["Portal"];
            string menuString = "<ul id='sddm'>";
            IList<Tab> topTabs = portal.Tabs.Where(x => x.ParentID == Guid.Empty).OrderBy(x => x.SortOrder).ToList();

            foreach (Tab tab in topTabs)
            {
                if (PortalSecurity.IsInRoles(tab.AuthorizedRoles) || PortalSecurity.IsInRole("1"))
                {
                    if (tab.ShowOnMenu)
                    {
                        IList<Tab> childTabs = portal.Tabs.Where(x => x.ParentID == tab.ID).OrderBy(x => x.SortOrder).ToList();
                        Guid divid = Guid.NewGuid();
                        if (childTabs.Count > 0)
                        {
                            menuString += string.Format("<li class='menu'><a href='{0}' onmouseover=\"mopen('{2}')\" onmouseout=\"mclosetime()\">{1}</a>", "#", tab.Name, divid);
                        }
                        else
                        {
                            menuString += string.Format("<li class='menu'><a href='{0}' onmouseover=\"mopen('{2}')\" onmouseout=\"mclosetime()\">{1}</a>", ResolveClientUrl("~/Default.aspx?tabid=" + tab.ID), tab.Name, divid);

                        }

                        if (childTabs.Count > 0)
                        {
                            menuString += string.Format("<div id='{0}' onmouseover='mcancelclosetime()' onmouseout='mclosetime()'>", divid);

                            foreach (Tab childTab in childTabs)
                            {
                                if (PortalSecurity.IsInRoles(tab.AuthorizedRoles) || PortalSecurity.IsInRole("1"))
                                {
                                    if (tab.ShowOnMenu || PortalSecurity.IsInRole("1"))
                                    {
                                        menuString += string.Format("<a href='{0}'>{1}</a>", ResolveClientUrl("~/Default.aspx?tabid=" + childTab.ID), childTab.Name);
                                    }
                                }
                            }
                            menuString += "</div>";
                        }
                        else
                        {
                            menuString += string.Format("<span style='visibility:hidden' id='{0}'></span>", divid);
                        }
                        menuString += "</li><li class='separation'><a href='#'>&nbsp;|&nbsp;</a></li>";
                    }
                }
            }
            menuString += "</ul>";

            const string cut = "<li class='separation'><a href='#'>&nbsp;|&nbsp;</a></li>";
            if (menuString.LastIndexOf(cut) > 0)
            {
                menuString = menuString.Substring(0, menuString.Length - cut.Length - 1);
            }
            writer.WriteLine(menuString);
            base.Render(writer);
        }
    }
}
